$(function(){
	$('#tt').datagrid({
		url:'/logStatisticsAction/getLogStatisticsActionListPage'
	});
	
});

function doAdd(){
	$('#addForm').form('clear');
	$('#addWin').window('open');
}

function doEdit(){
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	$('#idId').val(record.id);
	$('#nameId').val(record.name);
	$('#actionTableNameId').val(record.actionTableName);
	$('#conditionKeyId').val(record.conditionKey);
	$('#valueKeyId').val(record.valueKey);
	$('#syncTableNameId').val(record.syncTableName);
	$('#actionTypeId').combobox('setValue',record.actionType);
	$('#intervalId').combobox('setValue',record.interval);
	$('#addWin').window('open');
}

function doDel(){
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	if(record.status==1){
		$.messager.alert("提示", "启用状态的记录无法删除");
		return;
	}
	
	$.messager.confirm('提示', '你确定要删除记录吗', function(opt){
		if (opt){
			var logStatisticsActionId=record.id;
			$.ajax({
				url: '/logStatisticsAction/deleteLogStatisticsAction',
				type: 'POST',
				data : {
		            id:logStatisticsActionId,
		            delete_flag:1
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "删除成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
	
}

function doStatus(status){
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	if(record.status==status){
		if(status==1){
			$.messager.alert("提示", "记录已启动");
		}else{
			$.messager.alert("提示", "记录已暂停");
		}
		return;
	}
	
	$.messager.confirm('提示', '你确定要更改状态吗', function(opt){
		if (opt){
			var logStatisticsActionId=record.id;
			$.ajax({
				url: '/logStatisticsAction/updateStatus',
				type: 'POST',
				data : {
		            id:logStatisticsActionId,
		            status:status
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "操作成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
}



function doSearch(){
	var name=$('#snameId').val();
	var actionTableName=$('#sactionTableNameId').val();
	var status=$('#sstatusId').combobox('getValue');
	
	$('#tt').datagrid({
		url:'/logStatisticsAction/getLogStatisticsActionListPage',
		queryParams:{
			name:name,
			actionTableName:actionTableName,
			status:status
		}
	});
	
	$('#tt').datagrid('unselectAll');
}

function save(){
	var id=$('#idId').val();
	var url='/logStatisticsAction/updateLogStatisticsAction';
	if(id==null||id==''){
		url='/logStatisticsAction/saveLogStatisticsAction';
	}
	
	$('#addForm').form('submit',{
		url : url,
		onSubmit : function() {
			var flag = $(this).form('validate');
			if (!flag) {
				return flag;
			}
			showProcess(true, market.content.title, market.content.submitting);
			return true;
		},
		success : function(data) {
			showProcess(false);
			var data = eval('(' + data + ')');
			if (data.return_code == '1') {
				$("#addWin").window('close');
				$('#tt').datagrid('reload');
				$('#tt').datagrid('unselectAll');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		onLoadError : function() {
			showProcess(false);
			$.messager.alert(market.content.title, market.content.networkError);
		}
	});
	
}

function cancel(){
	$('#addWin').window('close');
}

function intervalFormatter(value){
	if(value==0){
		return '一直统计';
	}else if(value==1){
		return '按分钟统计'
	}else if(value==2){
		return '按15分钟统计'
	}else if(value==3){
		return '按小时统计'
	}else if(value==4){
		return '按天统计'
	}else{
		return '未知';
	}
}

function actionTypeFormatter(actionType){
	if(actionType==1){
		return '统计条数';
	}else if(actionType==2){
		return '统计平均值'
	}else if(actionType==3){
		return '统计总量值'
	}else if(actionType==4){
		return '自定义统计'
	}else{
		return '未知';
	}
}

function statusFormatter(status){
	if(status==1){
		return '启用';
	}else{
		return '未启用';
	}
}

